﻿using Shop.Core.Repository;
using Shop.Core.Share;
using Shop.IServices.Admin;
using Shop.Model.Dto.Admin.NewProduct;
using Shop.Model.Entity.Goo;
using Shop.Model.View.Admin.NewProduct;
using SqlSugar;

namespace Shop.Services.Admin;

public class NewProductService : AbstractService, INewProductService
{
    private readonly RepositoryBase<GoodsNewProduct> _newProductRepository;
    private readonly RepositoryBase<GoodsGoods> _goodsRepository;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="newProductRepository"></param>
    /// <param name="goodsRepository"></param>
    public NewProductService(RepositoryBase<GoodsNewProduct> newProductRepository,
        RepositoryBase<GoodsGoods> goodsRepository)
    {
        _newProductRepository = newProductRepository;
        _goodsRepository = goodsRepository;
    }

    /// <summary>
    /// 新品列表
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult<PageModel<NewProductListVo>> NewProductList(NewProductListDto dto)
    {
        var records = 0;
        var data = _newProductRepository.Queryable<GoodsNewProduct, GoodsGoods>((t1, t2) => new object[]
            {
                JoinType.Left, t1.Goods == t2.Id
            })
            .WhereIF(dto.Goods.HasValue, (t1, t2) => t1.Goods == dto.Goods.Value)
            .WhereIF(dto.IsDelete.HasValue, (t1, t2) => t1.IsDelete == dto.IsDelete)
            .OrderBy("t1.FoundTime DESC")
            .Select((t1, t2) => new NewProductListVo()
            {
                Id = t1.Id,
                Goods = t2.Name,
                Content = t1.Content,
                Spe = t1.Spe,
                Unit = t1.Unit,
                Title = t1.Title,
                FoundTime = t1.FoundTime,
                IsDelete = t1.IsDelete
            })
            .ToPageList(dto.PageIndex, dto.PageSize, ref records);
        return new PageModel<NewProductListVo>(dto, data, records);
    }

    /// <summary>
    /// 新品详情
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult<NewProductInfoVo> NewProductInfo(long dto)
    {
        var data = _newProductRepository.Queryable<GoodsNewProduct, GoodsGoods>((t1, t2) => new object[]
            {
                JoinType.Left, t1.Goods == t2.Id
            })
            .Where((t1, t2) => t1.Id == dto)
            .Select((t1, t2) => new NewProductInfoVo()
            {
                Id = t1.Id,
                Goods = t2.Name,
                Content = t1.Content,
                Spe = t1.Spe,
                Unit = t1.Unit,
                Img = t1.Img,
                Title = t1.Title,
                FoundTime = t1.FoundTime,
                IsDelete = t1.IsDelete
            })
            .First();
        return data;
    }

    /// <summary>
    /// 新品编辑
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult NewProductEdit(NewProductEditDto dto)
    {
        var newProduct = _newProductRepository.Where(x => x.Id == dto.Id).First();
        if (newProduct == null)
            return Problem("新品不存在");
        var goodsInfo = _goodsRepository.Where(x => x.Id == dto.Goods && x.Saleable == true && x.Valid == true).First();
        if (goodsInfo == null)
            return Problem("商品不存在");
        var newProductList = _newProductRepository.Where(x => x.IsDelete == false && x.Id != dto.Id).ToList();
        if (newProductList.Count <= 0)
        {
            if (!dto.IsDelete)
                return Problem("新品不可为空");
        }

        newProduct.Goods = dto.Goods;
        newProduct.Content = dto.Content;
        newProduct.Spe = dto.Spe;
        newProduct.Unit = dto.Unit;
        newProduct.Img = dto.Img;
        newProduct.Title = dto.Title;
        newProduct.IsDelete = dto.IsDelete;
        if (!dto.IsDelete)
        {
            if (newProductList.Count > 0)
            {
                newProductList.ForEach(x => x.IsDelete = true);
                _newProductRepository.UpdateRange(newProductList);
            }
        }

        _newProductRepository.Update(newProduct);
        return new AppResult();
    }

    /// <summary>
    /// 添加新品
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult NewProductAdd(NewProductAddDto dto)
    {
        var goodsInfo = _goodsRepository.Where(x => x.Id == dto.Goods && x.Saleable == true && x.Valid == true).First();
        if (goodsInfo == null)
            return Problem("商品不存在");
        var newProductList = _newProductRepository.Where(x => x.IsDelete == false).ToList();
        var newProductInfo = new GoodsNewProduct()
        {
            Goods = dto.Goods,
            Content = dto.Content,
            Spe = dto.Spe,
            Unit = dto.Unit,
            Img = dto.Img,
            Title = dto.Title,
            IsDelete = dto.IsDelete
        };
        if (!dto.IsDelete)
        {
            if (newProductList.Count > 0)
            {
                newProductList.ForEach(x => x.IsDelete = true);
                _newProductRepository.UpdateRange(newProductList);
            }
        }

        _newProductRepository.Insert(newProductInfo);
        return new AppResult();
    }

    /// <summary>
    /// 激活新品
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult NewProductEnable(long dto)
    {
        var newProduct = _newProductRepository.Where(x => x.Id == dto).First();
        if (newProduct == null)
            return Problem("新品不存在");
        if (newProduct.IsDelete == false)
            return Problem("新品已是激活状态,请勿重复操作");
        var newProductList = _newProductRepository.Where(x => x.IsDelete == false).ToList();
        newProduct.IsDelete = false;
        if (newProductList.Count > 0)
        {
            newProductList.ForEach(x => x.IsDelete = true);
            _newProductRepository.UpdateRange(newProductList);
        }

        _newProductRepository.Update(newProduct);
        return new AppResult();
    }
}